package PeterSheep.AccountsManage;

import PeterSheep.Kernel;
import PeterSheep.Utils.PEncryption;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.sql.SQLException;
import java.util.Arrays;

public class UactCommand implements CommandExecutor {
    private void CommandHelp(CommandSender commandSender, String[] cmds, String[] decs, String[] args) {
        // (?) Help command
        StringBuilder commandsHelpInformation = new StringBuilder();
        commandsHelpInformation.append("§6[UACT] §b: §7--------------- §c{ §2COMMANDS §c} §7--------------- §b:\n");
        commandsHelpInformation.append("  §e> Now available Command is: \n");
        for(int i = 0; i < cmds.length; i++) {
            commandsHelpInformation.append("    §3# §8").append(cmds[i]).append(" §6-> §8").append(decs[i]);
            commandsHelpInformation.append(" §5: §8").append(args[i]);
            commandsHelpInformation.append("\n");
        }

        commandsHelpInformation.append("§6[UACT] §b: §7----------------------------------------------- §b:\n");

        commandSender.sendMessage(commandsHelpInformation.toString());
    }

    @Override
    public boolean onCommand(CommandSender commandSender, Command command, String s, String[] strings) {
        Bukkit.getLogger().info(" (PeterSheep) --[#]> Issues command got, args: " + Arrays.toString(strings));

        if(!(commandSender instanceof Player))
        { commandSender.sendMessage("§4[CWM] §cFatal Error: CommandSender not a player!"); return false; }

        Player playerTypeCommandSender = (Player)commandSender;
        if(command.getName().equalsIgnoreCase("uact")) {
            String[] commandsList = {"register", "login", "forget-password", "password-process", "help"};
            String[] commandsArgsList = {"[<Subcommands>] (<Args...>)", "[<Email>] [<Password>]", "[<Password>]", "Null", "Null"};
            String[] commandsDescriptionsList = {"Register", "Login", "Forget password", "Password Encryption process", "Commander Helper"};

            if(strings.length == 0 || strings[0].equalsIgnoreCase("help"))
            { this.CommandHelp(commandSender, commandsList, commandsDescriptionsList, commandsArgsList); return true; }

            // @ Login command process
            else if(strings.length == 3 && strings[0].equalsIgnoreCase("login")) {
                boolean loginNoError = true;
                UAccount loginAccountPad = new UAccount();

                try {
                    Bukkit.getLogger().info(" (PeterSheep) --[?]> Login request after process password: "+PEncryption.passwordEncryption(strings[2]));
                    loginNoError = loginAccountPad.loginAccount(strings[1], strings[2]);
                    UBPlayer loginResult = new UBPlayer(playerTypeCommandSender, loginAccountPad);
                    Kernel.userAccountsList.addPlayerAccount(loginResult);
                }

                catch (SQLException | NoSuchAlgorithmException | UnsupportedEncodingException e) { e.printStackTrace(); }

                if(!loginNoError) {
                    playerTypeCommandSender.sendMessage("§6[UACT] §cCannot login account, maybe These errors occurred: ");
                    playerTypeCommandSender.sendMessage("§6[UACT]    §6# §cYour email or account is wrong.");
                    playerTypeCommandSender.sendMessage("§6[UACT]    §6# §cYour account not registered.");
                    playerTypeCommandSender.sendMessage("§6[UACT]    §6# §cAccount database not found.");
                }

                else {
                    playerTypeCommandSender.sendMessage("§6[UACT] §aLogin successes!");
                    playerTypeCommandSender.sendMessage("§6[UACT] §aWelcome login §b"+Kernel.pluginConfig.getString("serverName")
                            +" §e"+loginAccountPad.username);
                }
            }

            // @ Login command login message
            else if(strings.length < 3 && strings[0].equalsIgnoreCase("login")) {
                playerTypeCommandSender.sendMessage("§6[UACT] §cArgs not full!");
                playerTypeCommandSender.sendMessage("§6[UACT] §cLogin command need these args: §a"+commandsArgsList[1]);
                return true;
            }

            else if(strings.length < 3 && strings[0].equalsIgnoreCase("register")) {
                playerTypeCommandSender.sendMessage("§6[UACT] §cSorry, You cannot register at this time during the internal testing phase!");
                return true;
            }

            else if(strings.length == 2 && strings[0].equalsIgnoreCase("password-process")) {
                try {
                    String afterProcessPassword = PEncryption.passwordEncryption(strings[1]);
                    Bukkit.getLogger().info(" (PeterSheep) --[√]> Password process("+strings[1]+") result: "+afterProcessPassword);
                    playerTypeCommandSender.sendMessage("§6[UACT] §aRequest Processed, Find result in Server console!");
                }

                catch (UnsupportedEncodingException | NoSuchAlgorithmException e) { e.printStackTrace(); }
            }

            else { this.CommandHelp(commandSender, commandsList, commandsDescriptionsList, commandsArgsList); return true; }
        }

        return false;
    }
}
